package microcredit.model;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import microcredit.model.domain.Status;
import microcredit.model.util.ModelUtils;

/**
 * Entity implementation class for Entity: MemberToGroup
 *
 */

@Entity
public class MemberToGroup implements Serializable {

	
	private static final long serialVersionUID = 1L;

	public MemberToGroup() {
		super();
	}
   
	@Id
	@Column(name="MEMBER_ID", updatable=false, insertable=false)
	private Long memberID;
	
	@Id
	@Column(name="SEQUENCE_ID")
	private Long sequenceID;
	
	@Column(name="MEMBERGROUP_ID", updatable=false, insertable=false)
	private Long memberGroupID;
	
	@OneToOne(optional=true)
	private MemberGroup memberGroup;
	
	@ManyToOne
	private Member member;
	
	@Temporal(TemporalType.TIMESTAMP)
	@Column(nullable=false)
	private Date since;
	
	@Temporal(TemporalType.TIMESTAMP)
	@Column
	private Date until;
   
	@Enumerated(EnumType.STRING)
	@Column(length=1)
	private Status status;
	
	@Temporal(TemporalType.TIMESTAMP)
	@Column(nullable=false)
	private Date insertStamp;
	
	@Temporal(TemporalType.TIMESTAMP)
	@Column(nullable=false)
	private Date updateStamp;
	
	@PrePersist
	public void prePersist() throws ValidationException {
		if (getInsertStamp() == null) {
			setInsertStamp(ModelUtils.createTimestamp());
		}
		setUpdateStamp(ModelUtils.createTimestamp());
		validate();
	}
	
	@PreUpdate
	public void preUpdate() throws ValidationException {
		setUpdateStamp(ModelUtils.createTimestamp());
		validate();
	}
	
	public void validate() throws ValidationException {
		//TODO a lot more validations necessary
	}
	
	public Long getSequenceID() {
		return sequenceID;
	}

	public void setSequenceID(Long sequenceID) {
		this.sequenceID = sequenceID;
	}

	public Long getMemberGroupID() {
		return memberGroupID;
	}
	
	public Long getMemberID() {
		return memberID;
	}
	
	public Member getMember() {
		return member;
	}
	public void setMember(Member member) {
		this.member = member;
	}
	public MemberGroup getMemberGroup() {
		return memberGroup;
	}
	public void setMemberGroup(MemberGroup group) {
		this.memberGroup = group;
	}
	
	public Date getSince() {
		return since;
	}

	public void setSince(Date since) {
		this.since = since;
	}

	public Date getUntil() {
		return until;
	}

	public void setUntil(Date until) {
		this.until = until;
	}

	public Status getStatus() {
		return status;
	}
	public void setStatus(Status status) {
		this.status = status;
	}
	public Date getInsertStamp() {
		return insertStamp;
	}
	public void setInsertStamp(Date insertStamp) {
		this.insertStamp = insertStamp;
	}
	public Date getUpdateStamp() {
		return updateStamp;
	}
	public void setUpdateStamp(Date updateStamp) {
		this.updateStamp = updateStamp;
	}
	@Override
	public String toString() {
		if (member == null || memberGroup == null) {
			return "Member group assignment ";
		} else {
			return "Member group assignment ["+member.toString()+" -> "+memberGroup.toString()+"]";
		}
		
	}

}
